{include file="admin@public/header" /}

<link href="/public/static/admin/blue/bills.css?ver=20210803202401" rel="stylesheet" type="text/css">

<script src="/public/static/admin/js/common/base.js"></script>
<link rel="stylesheet" href="/public/static/admin/easyui/layui-v2.6.8/css/layui.css">
<style>
  /*#barCodeInsert{margin-left: 10px;font-weight: 100;font-size: 12px;color: #fff;background-color: #B1B1B1;padding: 0 5px;border-radius: 2px;line-height: 19px;height: 20px;display: inline-block;}*/
  /*#barCodeInsert.active{background-color: #23B317;}*/
  .h10 {
    height: 10px
  }

  .layui-form-radio {
    margin-top: 0;
  }

  .layui-form-radio>i:hover,
  .layui-form-radioed>i {
    color: #3CBAFF;
  }

  .edit-addr {
    text-decoration: underline;
    border-bottom: 1px solid #4dc2e6;
    padding: 3px 3px 5px 0;
    cursor: pointer;
  }

  .edit-addr:hover {
    border-bottom: 1px solid #4dc2e6;
  }

  .ui-icon-edit {
    background-position: 0 -160px !important;
  }

  .addr-drop-wrap {
    position: absolute;
    bottom: 0;
    right: -30px;
    top: 40px;
    z-index: 10;
    left: 23%;
    padding-right: 16px;
    opacity: 0;
    visibility: hidden;
    transition: all .3s;
  }

  .triangle-border,
  .triangle-bg {
    display: block;
    border-width: 8px;
    position: absolute;
    font-size: 0;
    line-height: 0;
    border-style: solid dashed dashed;
    border-color: transparent transparent #999;
    border-bottom: 9px solid #999;
    left: 15%;
    margin-left: -10px;
  }

  .triangle-border,
  .triangle-bg {
    top: -16px;
  }

  .addr-drop-wrap .addr-drop {
    width: 320px;
    border: 1px solid #e6e6e6;
    background: #fff;
    text-align: left;
    font-size: 12px;
    box-shadow: 0 2px 8px 0 rgba(204, 212, 222, .5);
    height: auto;
    min-height: 20px;
    padding: 10px 10px;
    border-radius: 8px;
  }

  .addr-drop-wrap .addr-drop .addrContent {
    display: block;
    width: 100%;
    word-wrap: break-word;
    word-break: normal;
    line-height: 20px !important;
  }
</style>

<div class="wrapper">
  <div class="mod-toolbar-top mr0 cf dn" id="toolTop"></div>
  <div class="bills cf">
    <div class="con-header layui-form">
      <dl class="cf">
        <dd class="mr20">
          <label><span class="red">*</span>客户:</label>
          <span class="ui-combo-wrap" id="customer" style="width: 198px;">
            <i class="ui-icon-ellipsis" onclick="chooseSupplier(1,1)"></i>
            <input type="text" name="" class="input-txt" autocomplete="off" value="" data-ref="date"
              style="width: 188px;">
          </span>
          <input type="hidden" id="customerid" value="" autocomplete="off" readonly="">
        </dd>
       
        <dd  class="mr10 tc" style="position: relative;">
          <label>销售人员:</label>
          <span class="ui-combo-wrap ui-combo-active" id="sales" style="width: 118px;">
            <input type="text" class="input-txt" autocomplete="off" style="width: 92px;" value="空" id="staff_name"><i
              class="trigger" id="choose_staff"></i></span>
            {include file="admin@public/staff" /}
        </dd>



        <dd class="mr10 tc">
          <label>单据日期:</label>
          <input type="date" id="dj_time" class="ui-input" value="{$today}" style="width: 125px;">
        </dd>

       <!-- 客户地址 -->
       {include file="admin@public/order/edit_address_input" /}


        {include file="admin@public/order/change_sn" /}
       <!-- 单据凭证 -->
        {include file="sales@pub/dj" /}
        <dd style="clear: both;"></dd>
       
      </dl>
      <hr class="hrcls" />
    </div>
    <div class="wrapper_con">
      <!-- <div class="cf mb10">
        <div class="fl">
          <span  id="config" class="ui-config"><a href="#" class="ui-icon-config-new"></a>列设置</span>
          <span><label>扫描枪录入</label><span id="barCodeInsert" class="close"><span class="ui-icon-circle"></span></span></span>
        </div>
        <div class="fr">
          <span class="circle-link-order linkToCheck"><a href="#" class="icon-link-order"></a>历史单据</span>
        </div>
      </div> -->
      <span id="config" class="ui-config"><a href="#" class="ui-icon-config-new"></a></span>
      <div class="grid-wrap">
        <table id="grid">
        </table>
        <div id="page"></div>
      </div>
      <div class="cf" id="bottomField">
        <span id="groupBtn">
          <a id="daoru" class="ui-btn ui-btn-search">批量导入</a>
          <a id="savaAndAdd" class="ui-btn ui-btn-sp">保存并新增</a>
          <a id="save" class="ui-btn">保存</a>
          <a id="o_shenhe" class="ui-btn">审核</a>
        </span>
        <div class="fr" id="toolBottom"></div>
      </div>
      <div id="mark"></div>
    </div>
    <div class="con-footer cf">
      <div class="mb10">
        <textarea type="text" id="note" class="ui-input ui-input-ph" placeholder="暂无备注信息"></textarea>
      </div>
       {include file="admin@public/yh" /}
       <ul  class="cf">
        {include file="admin@public/order/payment" /}
      </ul>

      <ul id="user-field">
        <li><label>快递单号：</label> <input type="text" class="ui-input ui-input-20" id="export"></li>
        <li class="udfLink"><a class="user-field">自定义字段</a></li>
      </ul>
      <ul id="user-field">
        <v-input v-for="(item, index) in udfData" :title="item.name" :type="item.type" v-model="item.value"
          :items="item.items" :metaid="item.metaId" :readonly="isChecked" :key="item.metaId"></v-input>
        <li class="udfLink">
          <v-user-field :transtype="transtype" :udfdata="udfData"></v-user-field>
        </li>
      </ul>


     
      <div class="cf fr">
        <a href="#" class="linkToCheck ui-btn-bill ui-btn-add mr20" onclick="addPage('/sales/invsa/index.html','sales_sa_index','销货单记录')">历史单据</a>
        <a href="#" id="operaLog" class="ui-btn">操作日志</a>
      </div>
    </div>

    <!-- 操作日志 -->
    <ul id="operaLogBox" class="shadow opera-log-box " style="position: absolute;z-index: 999; display: none;">
      <li>
        <span class="fl" style="margin-right: 50px;"><label>制单人：</label><span id="userName" style="margin-left: 12px;"><span open-data="" title="{$admin.name}">{$admin.name}</span></span></span>
    
    </ul>

  </div>
  {include file="admin@public/order/choose_good_sales" /}
  {include file="admin@public/order/choose_store" /}
</div>

<script type="text/javascript">
  //鼠标移开时隐藏展开的列表
  $('#toolBottom').on('mouseleave.menuEvent', '.ui-btn-menu', function (e) {
    $(this).removeClass('ui-btn-menu-cur');
  });
  $('#toolBottom').on('mouseover', '.ui-btn-menu', function (e) {
    $(this).addClass('ui-btn-menu-cur');
  });
</script>

<script type="text/javascript">
  $(document).ready(function () {
    // 操作日志鼠标移入移除
    $("#operaLog").hover(function () {
      var top = $(this).offset().top;
      // var left = $(this).offset().left;
      $("#operaLogBox").show();
      $("#operaLogBox").css({ "right": "30px", "top": (top + 20) + "px" })
    }, function () {
      $("#operaLogBox").hide();
    })
  });
</script>


<script type="text/javascript">
  var edit_row = 0;
  var edit_col = 0;
  var row_id = 0;
  var col_name = 0;
  $(document).ready(function () {

    var col2 = '<span class="red">*</span>商品<label> -- 扫描枪录入</label><span id="barCodeInsert" onclick="smq();" class="close"><span class="ui-icon-circle"></span></span>';
    var col5 = '<span id="outstore" onclick="outstore();"><span class="red">*</span>仓库<small id="batchStorageA">批量</small></span>';
    var col13 = '<span id="outstore" onclick="outstore();"><span class="red">*</span>折扣率(%)<small id="batchStorageA">批量</small></span>';
    var col14 = '<span id="outstore" onclick="outstore();"><span class="red">*</span>折扣(折)<small id="batchStorageA">批量</small></span>';
    var col15 = '<span id="outstore" onclick="outstore();"><span class="red">*</span>折扣额<small id="batchStorageA">批量</small></span>';


    $("#grid").jqGrid({

      datatype: "local",//数据来源，本地数据（local，json,jsonp,xml等）
      height: '90%',//高度，表格高度。可为数值、百分比或'auto'
      colNames: ['操作', col2, '商品备注', '单位', col5, '可用库存', '<span class="red">*</span>数量', "基本单位", '基本数量', '销售单价', '含税单价', '折后含税单价', col13, col14, col15, '折后单价', '金额', '税率（%）', '税额', '价税合计', '备注', '商品ID', '仓库ID','单位ID','基本单位'],
      colModel: [
        { name: '', index: 'id', width: 70, formatter: cLink ,align:'center' },

        {
          name: "good_name",
          label: "商品",
          width: 300,
          title: !0,

          classes: "goods",
          editable: true,
          edittype: "custom",
          editoptions: {
            custom_element: function (t, e) {
              $("#pelem_goods").html('<div class="pr" id="elem_goods"><input type="text" class="textbox  customelement" name="good_name" autocomplete="off" ><span class="ui-icon-ellipsis"></span></div>');
              return $("#elem_goods");
            },
            custom_value: function (t, e, a) {
              var ad = $("#good_name").val();
              $("#good_name").val("");//重置赋值
              return ad;
            },
            handle: function () {

            },
            trigger: "ui-icon-ellipsis"
          }
        },

        { name: 'good_mark', width: 100, editable: false },
        { name: 'unit_name', width: 100, editable: false },
        {
          name: "store_name",
          label: '<span class="red">*</span>仓库',
          nameExt: '<small id="batchStorage">批量</small>',
          width: 100,
          link: "inventory",
          editable: !0,
          edittype: "custom",
          editoptions: {
            custom_element: function (t, e) {
             
                $("#p_store_name_details").html('<div class="pr" id="store_name_details"><input value="'+t+'" type="text" class="textbox storageAuto customelement ui-combo-active" name="store_name" autocomplete="off" style="outline: -webkit-focus-ring-color auto 5px;"><span class="ui-icon-triangle-1-s store_name_details"></span></div>');
                return $("#store_name_details");
            },
            custom_value: function (t, e, a) {
              var ba = $("#store_name").val();
              $("#store_name").val("");//重置赋值
              return ba;
            },
            trigger: "ui-icon-triangle-1-s"
          }
        },
        { name: 'use_kc', width: 100, editable: false },
        { name: 'shuliang', width: 100,  label: "数量", editable: true },
        { name: 'base_unit_name', width: 100, editable: false },
        { name: 'base_number', width: 100, editable: false },
        { name: 'price', width: 100,  label: "销售单价", editable: true },
        { name: 'hs_price', width: 100,label: "含税单价", editable: true },
        { name: 'after_hs_price', width: 100 },
        { name: 'zk_lv', width: 100,label: "折扣率(%)", editable: true },
        { name: 'zk_zhe', width: 100,label: "折扣(折)", editable: true },
        { name: 'zk_e', width: 100, label: "折扣额", editable: true },
        { name: 'zk_price', width: 100 },
        { name: 'jine', width: 100, label: "金额", editable: true },
        { name: 'tax_lv', width: 100,  label: "税率", editable: true },
        { name: 'tax_e', width: 100, label: "税额", editable: true },
        { name: 'total_price', width: 100,label: "价税合计", editable: true },
        { name: 'mark', width: 100, editable: true },
        { name: 'good_id', width: 100, hidden: true },
        { name: 'store_id', width: 100, hidden: true },
        { name: 'unit_id', width: 100, hidden: true },
        { name: 'base_unit_id', width: 100, hidden: true },
      ],

      cellEdit: true,
      cellurl: '/',
      rowNum: 10,
      rowList: [10, 20, 30],
      sortname: 'id',
      sortorder: "desc",
      pagerpos: "left",//分页位置
      rownumbers:true,
      pgbuttons: true,//翻页按钮
      pagination: true,
      altRows: !0,
      gridview: !0,
      shrinkToFit: !1,
      cellLayout: 8,
      autowidth: !0,
      pager: "#page",
      viewrecords: !0,
      cmTemplate: {
        sortable: !1,
        title: !1
      },
      loadError: function (t, e, i) {
        parent.Public.tips({
          type: 1,
          content: "操作失败了哦，请检查您的网络链接！"
        })
      },
      beforeEditCell: function (rowid, cellname, value, iRow, iCol) {
        col_name = cellname;
        edit_row = iRow;
        edit_col = iCol;
        row_id = rowid;
        var gname = $("#grid").jqGrid("getCell", row_id, 'good_name');
        $("#good_name").val(gname);

        var store_namename = $("#grid").jqGrid("getCell", row_id, 'store_name');
        $("#store_namename").val(store_namename);
        // var ck2name=$("#grid").jqGrid("getCell",row_id,'ck2');
        // $("#ck2name").val(ck2name);
      },
      // 合计
      footerrow: true,
      gridComplete: function () {
        var shuliangs = $("#grid").getCol('shuliang', false, 'sum');
        var base_numbers = $("#grid").getCol('base_number', false, 'sum');
        var zk_es = $("#grid").getCol('zk_e', false, 'sum');
        var jines = $("#grid").getCol('jine', false, 'sum');
        var tax_es = $("#grid").getCol('tax_e', false, 'sum');
        var total_prices = $("#grid").getCol('total_price', false, 'sum');
        $("#yh_price").val(total_prices);//优惠后金额
        $("#before_yh_price").val(total_prices);
        $("#payment").val(total_prices);//本次收款

        $("#grid").footerData('set', { "good_name": '合计', shuliang: shuliangs, base_number: base_numbers, zk_e: zk_es, jine: jines, tax_e: tax_es, total_price: total_prices });
      },
      beforeSaveCell: function (rowid, cellname, value, iRow, iCol) {
        reload_val(rowid,cellname);
      },
    });

    var mydata = [];
    for (var i = 0; i < 5; i++) {
      var obj = { id: i + 1 ,}
      mydata.push(obj);
    };


    for (var i = 0; i <= mydata.length; i++) {
      jQuery("#grid").jqGrid('addRowData', i + 1, mydata[i]);
    }

    //增加、删除按钮
    function cLink(cellvalue, options, rowObject) {
      return '<div class="operating"><span class="ui-icon ui-icon-plus" title="新增行" onclick="add_row(' + options.rowId + ')" style="margin:0 2px;"></span> <span class="ui-icon ui-icon-trash" onclick="del_row(' + options.rowId + ')" title="删除行" style="margin:0 2px;">删除</span></div>';
    }



     // 选择仓库弹窗
    $(".grid-wrap").on("click", ".store_name_details", function (e) {
      var top = $(this).offset().top;
      var left = $(this).offset().left;
      $("#droplist_store").show();
      $("#droplist_store").css({ "top": (top + 22) + "px", "left": (left - 78) + "px" })
    })

  });



</script>

<script type="text/javascript">

  function reload_val() {
    var rid = row_id;
    var c_name = col_name;

    var $shuliang = parseInt($("#grid").jqGrid("getCell", rid, 'shuliang'));//数量
    var $price = parseFloat($("#grid").jqGrid("getCell", rid, 'price'));//单价

    var $tax_lv = parseFloat($("#grid").jqGrid("getCell", rid, 'tax_lv'));//税率
    var $tax_lv1 = 100 + $tax_lv;

    var $zk_lv = parseFloat($("#grid").jqGrid("getCell", rid, 'zk_lv'));//折扣率
    var $zk_lv1 = 100 - $zk_lv;



    if (c_name == "shuliang") {
      //改变数量
      $("#grid").jqGrid("setCell", rid, 'base_number', $shuliang);
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额--折后单价乘数量
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'zk_e', $price * $zk_lv / 100 * $shuliang);//折扣额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计

    } else if (c_name == "price") {
      //改变单价
      $("#grid").jqGrid("setCell", rid, 'hs_price', $price * $tax_lv1 / 100);//含税单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'zk_price', $price * $zk_lv1 / 100);//折后单价
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计
      $("#grid").jqGrid("setCell", rid, 'zk_e', $price * $zk_lv / 100 * $shuliang);//折扣额

    } else if (c_name == "hs_price") {
      //改变含税单价
      var $hs_price = parseFloat($("#grid").jqGrid("getCell", rid, 'hs_price'));//含税单价
      $price = $hs_price * 100 / $tax_lv1;//单价
      $("#grid").jqGrid("setCell", rid, 'price', $price);//单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'zk_price', $price * $zk_lv1 / 100);//折后单价
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计
      $("#grid").jqGrid("setCell", rid, 'zk_e', $price * $zk_lv / 100 * $shuliang);//折扣额

    } else if (c_name == "tax_lv") {
      //改变税率
      $("#grid").jqGrid("setCell", rid, 'hs_price', $price * $tax_lv1 / 100);//含税单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计

    } else if (c_name == "zk_lv") {
      //改变折扣率
      $("#grid").jqGrid("setCell", rid, 'zk_zhe', (100 - $zk_lv) / 10);//打几折
      $("#grid").jqGrid("setCell", rid, 'zk_e', $price * $zk_lv / 100 * $shuliang);//折扣额
      $("#grid").jqGrid("setCell", rid, 'zk_price', $price * $zk_lv1 / 100);//折后单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计

    } else if (c_name == "zk_zhe") {
      //改变折扣-打几折
      var $zk_zhe = parseFloat($("#grid").jqGrid("getCell", rid, 'zk_zhe'));//打几折
      $zk_lv = 100 - ($zk_zhe * 10);//折扣率
      $zk_lv1 = 100 - $zk_lv;
      $("#grid").jqGrid("setCell", rid, 'zk_lv', $zk_lv);//折扣率
      $("#grid").jqGrid("setCell", rid, 'zk_e', $price * $zk_lv / 100 * $shuliang);//折扣额
      $("#grid").jqGrid("setCell", rid, 'zk_price', $price * $zk_lv1 / 100);//折后单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计

    } else if (c_name == "zk_e") {
      //改变折扣额
      var $zk_e = parseFloat($("#grid").jqGrid("getCell", rid, 'zk_e'));//折扣额
      $zk_lv = $zk_e / $shuliang * 100 / $price;//折扣率
      $zk_lv1 = 100 - $zk_lv;
      $("#grid").jqGrid("setCell", rid, 'zk_lv', $zk_lv);//折扣率
      $("#grid").jqGrid("setCell", rid, 'zk_zhe', (100 - $zk_lv) / 10);//打几折
      $("#grid").jqGrid("setCell", rid, 'zk_price', $price * $zk_lv1 / 100);//折后单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计

    } else if (c_name == "jine") {
      //改变金额
      var $jine = parseFloat($("#grid").jqGrid("getCell", rid, 'jine'));//金额
      $price = $jine / $shuliang * 100 / $zk_lv1;//销售单价
      $("#grid").jqGrid("setCell", rid, 'price', $price);//单价
      $("#grid").jqGrid("setCell", rid, 'hs_price', $price * $tax_lv1 / 100);//含税单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'zk_price', $price * $zk_lv1 / 100);//折后单价
      // $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计
      $("#grid").jqGrid("setCell", rid, 'zk_e', $price * $zk_lv / 100 * $shuliang);//折扣额

    } else if (c_name == "tax_e") {
      //改变税额
      var $tax_e = parseFloat($("#grid").jqGrid("getCell", rid, 'tax_e'));//税额
      $tax_lv = $tax_e / $shuliang * 100 / $price / $zk_lv1 * 100;
      $tax_lv1 = 100 + $tax_lv;
      $("#grid").jqGrid("setCell", rid, 'tax_lv', $tax_lv);//税率
      $("#grid").jqGrid("setCell", rid, 'hs_price', $price * $tax_lv1 / 100);//含税单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      //$("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100*$shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计

    } else if (c_name == "total_price") {
      //改变价税合计
      var $total_price = parseFloat($("#grid").jqGrid("getCell", rid, 'total_price'));//价税合计
      $price = $total_price / $shuliang * 100 / $tax_lv1 * 100 / $zk_lv1;//单价
      $("#grid").jqGrid("setCell", rid, 'price', $price);//单价
      $("#grid").jqGrid("setCell", rid, 'hs_price', $price * $tax_lv1 / 100);//含税单价
      $("#grid").jqGrid("setCell", rid, 'after_hs_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100);//折后含税单价
      $("#grid").jqGrid("setCell", rid, 'zk_price', $price * $zk_lv1 / 100);//折后单价
      $("#grid").jqGrid("setCell", rid, 'jine', $price * $zk_lv1 / 100 * $shuliang);//金额
      $("#grid").jqGrid("setCell", rid, 'tax_e', $price * $zk_lv1 / 100 * $tax_lv / 100 * $shuliang);//税额
      $("#grid").jqGrid("setCell", rid, 'total_price', $price * $zk_lv1 / 100 * $tax_lv1 / 100 * $shuliang);//价税合计
      $("#grid").jqGrid("setCell", rid, 'zk_e', $price * $zk_lv / 100 * $shuliang);//折扣额

    }

    // 合计的变量


    var shuliangs = $("#grid").getCol('shuliang', false, 'sum');
    var base_numbers = $("#grid").getCol('base_number', false, 'sum');
    var zk_es = $("#grid").getCol('zk_e', false, 'sum');
    var jines = $("#grid").getCol('jine', false, 'sum');
    var tax_es = $("#grid").getCol('tax_e', false, 'sum');
    var total_prices = $("#grid").getCol('total_price', false, 'sum');
    $("#yh_price").val(total_prices);
    $("#before_yh_price").val(total_prices);
    $("#payment").val(total_prices);//本次收款
    onchange_payment();

    $("#grid").footerData('set', { "good_name": '合计', shuliang: shuliangs, base_number: base_numbers, zk_e: zk_es, jine: jines, tax_e: tax_es, total_price: total_prices });
  }
  


</script>


<script type="text/javascript">
  var grid = $('#grid');

  $(".wrapper").on("click", "#savaAndAdd", function (e) {
    //先保存
    save_jqgrid(edit_row, edit_col, grid, function (res) {
      savedata(1);

    });
  })

  $(".wrapper").on("click", "#o_shenhe", function (e) {
    //先保存
    save_jqgrid(edit_row, edit_col, grid, function (res) {
      savedata(4);
      
    });
  })



  $(".wrapper").on("click", "#save", function (e) {
    save_jqgrid(edit_row, edit_col, grid, function (res) {
      $("#grid").jqGrid().trigger("reloadGrid");
      layer.msg("保存成功");
    });
  })
  //1.添加，2编辑，3复制，4，审核订单
  function savedata(type_btn) {

    var gooddata = $('#grid').jqGrid('getGridParam', 'data');
    var flag = 0;
    var hasuser = 0;
    var hasuser1 = 0;
    var indexArr = [];
    console.log(gooddata);
    gooddata.forEach(function (self, index) {
      //console.log(self);   ""1
      if (self['good_id'] != null && self['good_id'] != "") {
        //console.log("good");
        hasuser = hasuser + 1;
        if (self['store_id'] == null || self['store_id']=="") {
          flag = 1;
          indexArr.push(index);
        }else{
           hasuser1 = hasuser1 + 1;
        }
      }
    });
    if (hasuser == 0) {
      layer.msg("请选择商品");
      // 必填项（商品）
      $("#grid").editCell(1, 2, true);
      return false;
    }

    if (flag == 1) {
      layer.msg("请选择仓库");
      // 必填项（仓库）
      $("#grid").editCell(indexArr[0]+1, 5, true);
      return false;
    }

    var customerid = $("#customerid").val();//客户ID
    if (customerid == "") {
      layer.msg('请先选择客户');
      return false;
    }

    var account_id = $("#account_id").val();//结算账户
    var accountdata = [];

    var purchase_data = JSON.parse(localStorage.getItem("purchase_data"));//采购数据

    if (account_id == -1) {
      var account_total_price = $("#account_total_price").val();
      if (payment != account_total_price) {
        layer.msg("多账户金额不等于本次收款金额");
        return false;
      }
      accountdata = JSON.parse(localStorage.getItem("more_account_data"));//多账户 
    }
    var movedata = gooddata;
    var dj_time = $("#dj_time").val();//单据日期
   // var jh_time = $("#jh_time").val();//交货日期
    var sn = $("#number").html();//单据号
    var mark = $("#note").val();//备注
    var type = $('input:radio[name="type"]:checked').val();//业务类别
    var yh_lv = $("#yh_lv").val();//优惠率
    var yh_e = $("#yh_e").val();//优惠额
    var yh_price = $("#yh_price").val();//优惠后价格
    var before_yh_price = $("#before_yh_price").val();//优惠前价格

    var payment = $("#payment").val();//本次收款
    var arrears = $("#arrears").val();//本次欠款
    var total_arrears = $("#total_arrears").val();//累计欠款

    var kddh = $("#export").val();//快递单号
    var addr = $("#addr").val();//客户地址
    var by_admin = $("#staff_id").val();//销售人员

    var user_cd_price = $("#user_cd_price").val();//客户承担费用
    var saler_user_price = $("#saler_user_price").val();//销售费用
    var imgs = $("#imgs").val();//单据凭证

    var type = 0;//是否是退货

    if (type_btn == 1) {
      //新增
      var param = { before_yh_price: before_yh_price, user_id: customerid, sn: sn, mark: mark, movegood: movedata, dj_time: dj_time,  yh_lv: yh_lv, yh_e: yh_e, yh_price: yh_price, type: type,payment:payment,arrears:arrears ,total_arrears:total_arrears, account: account_id, accountdata:accountdata,by_admin:by_admin,export:kddh,addr:addr,user_cd_price:user_cd_price,saler_user_price:saler_user_price,imgs:imgs};
      var url = "save.html";
    } else if (type_btn == 4) {
      //直接审核
      var param = { o_shenhe: 1, before_yh_price: before_yh_price, user_id: customerid, sn: sn, mark: mark, movegood: movedata, dj_time: dj_time, yh_lv: yh_lv, yh_e: yh_e, yh_price: yh_price, type: type,payment:payment,arrears:arrears ,total_arrears:total_arrears, account: account_id, accountdata:accountdata, by_admin:by_admin,export:kddh,addr:addr,user_cd_price:user_cd_price,saler_user_price:saler_user_price,imgs:imgs};
      var url = "save.html";

    }

    $.ajax({
      type: 'POST',
      url: url,
      data: param,
      dataType: "json",
      async: false,
      error: function (request) {
        layer.msg("发送请求失败！");
      },
      success: function (data) {
        if (data.status == 1) {
          if(type_btn==4){
            layer.msg("审核成功！");
            if(data.editId && data.editId != 0 ){
              setTimeout(function () {
                location.href="/sales/invsa/edit.html?id="+data.editId;
              }, 2900);
            }
          }else{
            layer.alert(data.msg, function () {
              location.reload();
            });
          }
          
        } else {
          layer.msg(data.msg);
        }

      }
    });

  }


</script>



{include file="admin@public/footer" /}